<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>体重选择</title>
    <style>
        .box {
            width: 100%;
            height: 100px;
            box-shadow: 0px 0px 10px pink;
            border-radius: 10px;
            overflow: hidden;

            margin-top: 100px;
        }

        /* 滚动的 */
        .box-inner {
            position: absolute;
            /* 宽度等于父元素的高度 */
            width: var(--h);
            /* 高度等于父元素的宽度 */
            height: var(--w);
            transform-origin: 0 0;
            transform: translateY(var(--h)) rotate(-90deg);
            position: relative;
            overflow: auto;
            /* scroll-behavior: smooth; */
        }

        /* 内容区域 */
        .box-inner-content {
            width: auto;
            /* 高度不可以随父元素的高；要是最外层的高box */
            height: var(--h);
            position: absolute;
            left: 100%;
            transform-origin: 0 0;
            /* 用于显示内容的；角度旋转90deg -90 - 90 = 0 恢复到正常 */
            transform: rotate(90deg);
        }

        .box-inner::-webkit-scrollbar {
            width: 0px;
        }

        .content {
            display: inline-flex;
            height: 100%;
        }

        .card-item {
            height: 100%;
            width: 300px;
            background-color: skyblue;
            font-size: 30px;
            display: flex;
            justify-content: center;
            align-items: center;
            user-select: none;
        }

        .card-item+.card-item {
            margin-left: 20px;
        }
    </style>
</head>

<body>
    <div class="box" id="box">
        <div class="box-inner" id="scrollBox">
            <div class="box-inner-content">
                <div class="content">
                    <div class="card-item">name1</div>
                    <div class="card-item">name1</div>
                    <div class="card-item">name1</div>
                    <div class="card-item">name1</div>
                    <div class="card-item">name1</div>
                    <div class="card-item">name1</div>
                    <div class="card-item">name1</div>
                    <div class="card-item">name1</div>
                    <div class="card-item">name1</div>
                    <div class="card-item">name1</div>
                    <div class="card-item">name1</div>
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    let box = document.querySelector("#box");
    let scrollBox = document.querySelector("#scrollBox");
    const elRectSetVars = (element) => {
        let rect = {};
        const onResize = () => {
            rect['width'] = element.clientWidth + 'px';
            rect['height'] = element.clientHeight + 'px';
            rect['w'] = element.clientWidth;
            rect['h'] = element.clientHeight;
            element.style.setProperty('--w', rect.width);
            element.style.setProperty('--h', rect.height);
        }
        const resizeObserve = new ResizeObserver(() => onResize());
        resizeObserve.observe(element);
    }
    elRectSetVars(box);

    const scrollBoxPosition = {
        startX:0,
        startY:0
    }
    const scrollBoxDown = (event) => {
        scrollBoxPosition.startX = event.clientX + scrollBox.scrollTop;
        scrollBoxPosition.startY = event.clientY + scrollBox.scrollLeft;
        scrollBox.addEventListener('mousemove', scrollBoxMove)
    }
    const scrollBoxMove = (event) => {
        let moveX = event.clientX - scrollBoxPosition.startX;
        let moveY = event.clientY - scrollBoxPosition.startY;
        scrollBox.scrollTop = -moveX;
    }
    const scrollBoxUp = (event) => {
        scrollBox.removeEventListener('mousemove', scrollBoxMove)
    }
    scrollBox.addEventListener('mousedown', scrollBoxDown);
    scrollBox.addEventListener('mouseup', scrollBoxUp);
    const removeMouseMoveEvents = (element,fun) => {
        // 监听鼠标离开窗口的事件
        element.addEventListener('mouseleave', () => {
            element.removeEventListener('mousemove', fun);
        });
        // 可选：也可以监听整个窗口的鼠标离开事件
        window.addEventListener('blur', () => {
            element.removeEventListener('mousemove', fun);
        });
        // 监听页面可见性变化
        document.addEventListener('visibilitychange', () => {
            if (document.hidden) {
                element.removeEventListener('mousemove', fun);
            }
        });
    }
    removeMouseMoveEvents(scrollBox,scrollBoxMove);
</script>

</html>